package com.guanyong.webapi.controllers.api;

import com.guanyong.model.KeywordModel;
import com.guanyong.model.UserBaseModel;
import com.guanyong.services.KeyWordService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.webapi.handle.UserHandle;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by mxk on 2017/7/3.
 */

@RestController
@RequestMapping("/api/keyword/")
public class KeyWordApi {
    @Autowired
    KeyWordService keyWordService;

    @Autowired
    UserHandle userHandle;

    @RequestMapping(value = "getkeywordlist", method = RequestMethod.GET)
    ResultObject<List<KeywordModel>> getkeywordlist(HttpServletRequest request,
                                                    @RequestParam Integer count) {
        ResultObject<List<KeywordModel>> resultObject=new ResultObject<List<KeywordModel>>();
        List<KeywordModel> list= keyWordService.GetHotKeyWord(count);
        resultObject.setResult(list);
        return  resultObject;
    }

    @RequestMapping(value = "getkeywordlist1_0", method = RequestMethod.GET)
    ResultObject<List<KeywordModel>> getkeywordlist1_0(HttpServletRequest request,
                                                    @RequestParam Integer count) {
        ResultObject<List<KeywordModel>> resultObject=new ResultObject<List<KeywordModel>>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        List<KeywordModel> list=null;
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            list= keyWordService.GetHotKeyWord(count,userBaseModel.getfId());
        }

        resultObject.setResult(list);
        return  resultObject;
    }

    @RequestMapping(value = "addkeyword", method = RequestMethod.POST)
    ResultObject<Integer> addkeyword(HttpServletRequest request,
                                                    @RequestParam String keyword) {
        if (keyword==null||"".equals(keyword.trim())){
            ResultObject<Integer> resultObject=new ResultObject<Integer>();
            resultObject.setCode(ResultCode.OperateOk);
        }
        KeywordModel keywordModel=new KeywordModel();
        keywordModel.setfKeyword(keyword);
        String keywordMd5=com.guanyong.utils.Encrypt.EncryptExt.GetMd5Code(keyword);
        keywordModel.setfKeywordmd5(keywordMd5);
        return  keyWordService.AddKeyWord(keywordModel);
    }

    @RequestMapping(value = "addkeyword1_0", method = RequestMethod.POST)
    ResultObject<Integer> addkeyword1_0(HttpServletRequest request,
                                     @RequestParam String keyword) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        if (keyword==null||"".equals(keyword.trim())){
            resultObject.setCode(ResultCode.OperateOk);
        }

        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        KeywordModel keywordModel=new KeywordModel();
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            keywordModel.setfKeyword(keyword);
            String keywordMd5=com.guanyong.utils.Encrypt.EncryptExt.GetMd5Code(keyword);
            keywordModel.setfKeywordmd5(keywordMd5);
            keywordModel.setfUserid(userBaseModel.getfId());
            resultObject =keyWordService.AddKeyWord1_0(keywordModel);
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }

        return resultObject ;
    }
}
